package com.toman.Adpter;

import android.content.Context;
import android.graphics.Color;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

import com.toman.R;
import com.toman.Util.StringUtils;
import com.toman.Util.ToastUtil;
import com.toman.Util.ViewUtil;
import com.toman.bean.ItemTypeBean;

import java.util.List;

/**
 * Created by 邹苏隆 on 2018/5/10.
 * description：
 */

public class ItemTypeCheckAdapter extends AdapterBase<ItemTypeBean, ItemTypeCheckHolder> implements View.OnClickListener, CompoundButton.OnCheckedChangeListener {
    private boolean editable = false;
    public boolean first = false;
    public interface ActionClickListener {
        public void onDetailClick(ItemTypeBean bean);

        //查看详情
        public void onCheckClick(ItemTypeBean bean);
//审核
    }

    private LayoutInflater inflater;
    private Context mContext;
    private ActionClickListener actionClickListener;

    public void setActionClickListener(ActionClickListener l) {
        actionClickListener = l;
    }

    public ItemTypeCheckAdapter(Context context) {
        inflater = LayoutInflater.from(context);
        this.mContext = context;

    }


    @Override
    protected void handlerData(final List<ItemTypeBean> mList2, final int position, final ItemTypeCheckHolder itemView) {
        ItemTypeBean bean = mList2.get(position);
        itemView.tv_name.setText(bean.getName());
        itemView.tv_more.setOnClickListener(this);
        itemView.et_content.setTag(R.id.b, position);
        itemView.tv_more.setTag(position);
//        不先清空监听器的话，按钮显示会出问题
        itemView.sw_status.setOnCheckedChangeListener(null);
        itemView.et_content.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                ((ViewGroup) v.getParent())
                        .setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
                return false;
            }
        });


        if (bean.getType() == null) {
            if (bean.getPassIf().equals("0")) {
                itemView.sw_status.setChecked(true);
                itemView.tv_status.setText("通过");
                itemView.tv_status.setTextColor(Color.parseColor("#53D666"));

            } else {
                itemView.sw_status.setChecked(false);
                itemView.tv_status.setText("不通过");
                itemView.tv_status.setTextColor(Color.parseColor("#CCCCCE"));
            }
            itemView.sw_status.setTag(position);

            itemView.sw_status.setOnCheckedChangeListener(this);
        } else {

            bean.setItemId(bean.getId());
            switch (bean.getType()) {
                case "1":
                    itemView.sw_status.setVisibility(View.VISIBLE);
                    itemView.et_content.setVisibility(View.INVISIBLE);
                    if (null == bean.getPassIf() || bean.getPassIf().equals("0")) {
                        itemView.sw_status.setChecked(true);
                        itemView.tv_status.setText("通过");
                        itemView.tv_status.setTextColor(Color.parseColor("#53D666"));

                    } else {
                        itemView.sw_status.setChecked(false);
                        itemView.tv_status.setText("不通过");
                        itemView.tv_status.setTextColor(Color.parseColor("#CCCCCE"));
                    }
                    itemView.sw_status.setTag(position);

                    itemView.sw_status.setOnCheckedChangeListener(this);
                    break;
                case "2":
//                    itemView.et_content.setTag(position);
                    itemView.sw_status.setVisibility(View.INVISIBLE);
                    itemView.et_content.setVisibility(View.VISIBLE);
                    try {
                        if (StringUtils.isEmpty(bean.getRangeInputValue())) {
                            itemView.et_content.setText(bean.getDefaultNumber());
                            compare(Double.valueOf(bean.getDefaultNumber()), Double.valueOf(bean.getMinNumber()), Double.valueOf(bean.getMaxNumber()), itemView, bean);

                        } else {
                            itemView.et_content.setText(bean.getRangeInputValue());
                            compare(Double.valueOf(bean.getRangeInputValue()), Double.valueOf(bean.getMinNumber()), Double.valueOf(bean.getMaxNumber()), itemView, bean);

                        }
                    } catch (Exception e) {

                    }
                    if (editable&&bean.getCheckIf().equals("0")) {
                        ViewUtil.edit(itemView.et_content);

                    } else {
                        ViewUtil.unedit(itemView.et_content);
                    }
                    double min = Double.valueOf(bean.getMinNumber());
                    double max = Double.valueOf(bean.getMaxNumber());
                    double num = 0;
                    try {
                        num = Double.valueOf(bean.getDefaultNumber());
                    } catch (NumberFormatException e) {
                        e.printStackTrace();
                    }
//                    compare(num, min, max, itemView, bean);
                    TextWatcher textWatcher = (TextWatcher) itemView.et_content.getTag(R.id.a);
                    if (textWatcher == null) {
                        itemView.et_content.addTextChangedListener(new TextWatcher() {
                            @Override
                            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                            }

                            @Override
                            public void onTextChanged(CharSequence s, int start, int before, int count) {

                            }

                            @Override
                            public void afterTextChanged(Editable s) {

                                int mPos = (int) itemView.et_content.getTag(R.id.b);
                                if (mPos == position) {

                                    ItemTypeBean bean = mList2.get(mPos);
                                    bean.setRangeInputValue(s.toString());
                                    double num = 0;
                                    ToastUtil.print("现在的位置" + position);
                                    try {
                                        num = Double.valueOf(s.toString());
                                        bean.setRangeInputValue(num + "");
                                    } catch (NumberFormatException e) {
                                        e.printStackTrace();
                                        bean.setRangeInputValue(null);
                                    }

                                    compare(num, min, max, itemView, bean);

                                }
                            }

                        });

                        itemView.et_content.setTag(R.id.a, textWatcher);
                    }


                    break;

                default:


                    itemView.sw_status.setChecked(true);
                    itemView.tv_status.setText("通过");
                    itemView.tv_status.setTextColor(Color.parseColor("#53D666"));
                    bean.setPassIf("0");
                    bean.setCheck_status("0");


                    itemView.sw_status.setTag(position);

                    itemView.sw_status.setOnCheckedChangeListener(this);

                    break;
            }

        }
//        设置点检
        if (bean.getCheckIf().equals("0")){
            itemView.rl.setBackgroundColor(Color.WHITE);
        }
        else {
            itemView.rl.setBackgroundColor(mContext.getResources().getColor(R.color.line_gray));
            itemView.tv_status.setText("不点检");
            itemView.tv_status.setTextColor(Color.parseColor("#CCCCCE"));

        }


    }

    private void compare(double num, double min, double max, ItemTypeCheckHolder itemView, ItemTypeBean bean) {
        if (num >= min && num <= max) {
            itemView.tv_status.setText("通过");
            itemView.tv_status.setTextColor(Color.parseColor("#53D666"));
            bean.setPassIf("0");
        } else {
            itemView.tv_status.setText("不通过");
            itemView.tv_status.setTextColor(Color.parseColor("#CCCCCE"));
            bean.setPassIf("1");

        }
    }

    @Override
    protected ItemTypeCheckHolder getItemViewHolder() {
        return new ItemTypeCheckHolder();
    }

    @Override
    protected View initConvertView(View convertView, ItemTypeCheckHolder v) {
        convertView = inflater.inflate(R.layout.item_item_check, null);

        convertView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                ((ViewGroup) v)
                        .setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
                return false;
            }
        });
       v.rl = (RelativeLayout) convertView
                .findViewById(R.id.rl);
        v.tv_name = (TextView) convertView
                .findViewById(R.id.tv_name);
        v.et_content = (EditText) convertView
                .findViewById(R.id.et_content);
        v.tv_status = (TextView) convertView
                .findViewById(R.id.tv_status);
        v.tv_more = (TextView) convertView
                .findViewById(R.id.tv_more);
        v.sw_status = (Switch) convertView.findViewById(R.id.sw_status);
        return convertView;
    }

    @Override
    protected void onReachBottom() {

    }

    @Override
    public void onClick(View view) {
        int position = (Integer) view.getTag();
        ItemTypeBean bean = getItem(position);

        switch (view.getId()) {
            case R.id.tv_more:
                if (actionClickListener != null) {
                    actionClickListener.onDetailClick(bean);
                }
                break;


        }

    }

    @Override
    public void onCheckedChanged(CompoundButton view, boolean b) {

        int position = (Integer) view.getTag();
        ItemTypeBean bean = getItem(position);

        if (bean.getCheckIf().equals("1")){
            view.setChecked(!b);
        }

  else       if (editable) {

            if (b) {
                bean.setCheck_status("0");
                bean.setPassIf("0");
                notifyDataSetChanged();
            } else {
                bean.setCheck_status("1");
                bean.setPassIf("1");
                notifyDataSetChanged();
            }
        } else {
            view.setChecked(!b);
        }

    }

    public void isEdit(boolean b) {
        editable = b;
    }
}


class ItemTypeCheckHolder {
    RelativeLayout rl;
    TextView tv_name;
    TextView tv_more;
    TextView tv_status;
    Switch sw_status;
    EditText et_content;
}
